﻿picShare.service('loginSvc', function (httpRequestSvc, userDataSvc, pageLocatorSvc, albumsSvc) {

    var loginUser = function (userName, password) {
        var calculateHash = CryptoJS.SHA1(password);
        var hash = calculateHash.toString();

        var user = {
            userName: userName,
            authCode: hash,
        };

        httpRequestSvc.postJson(httpRequestSvc.httpRootUserUrl + "login", user).done(
            function (data) {
                userDataSvc.putUserInfo(data.id, data.userName, data.sessionKey, data.albumNames);
                albumsSvc.loadUserAlbums();
                pageLocatorSvc.goTo("/albums");
            }, function (err) {
                alert(err);
            });
        };

    return {
        logUser: function (userName, password) {
            return loginUser(userName, password);
        }
    };
});